package com.adou.d9.udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        // 1.创建客户端对象
        DatagramSocket socket = new DatagramSocket();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请说：");
            String msg = sc.nextLine();

            // 一旦输出"exit",就退出
            if("exit".equals(msg)) {
                System.out.println("欢迎下次光临，退出成功");
                socket.close();
                break;
            }


            // 2.创建数据包对象封装发出去数据
//            byte[] bytes = "我是快乐的客户端！！！".getBytes();
            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 6666);

            // 3.发送数据
            socket.send(packet);
        }


        System.out.println("客户端数据发放完毕...");
        // 4.释放资源
        socket.close();
    }
}
